<template>
  <div class="type-nav">
    <div class="container">
      <div @mouseenter="isInNavList = true" @mouseleave="isInNavList = false">
        <h2 class="all">全部商品分类</h2>
        <transition name="nav">
          <div
            class="sort"
            @mouseleave="mouseleaveHandler"
            @mouseenter="isInCategory = true"
            @click="toSearchRoute"
            v-show="isInSearch"
          >
            <div class="all-sort-list2">
              <!-- 一级分类列表 -->
              <!-- 
            做节流:
              - 给所有列表绑定的mouseenter事件,事件函数都是同一个
              - 我们希望这个事件函数进行节流操作
              - 节流操作之后,不管移入到哪一个列表元素,都要判断这个函数是否到了执行的时机
              - 因为鼠标移入到列表元素之后,就要发送对应的二级分类列表,所以我们希望节流来执行函数
           -->
              <div
                class="item"
                v-for="(category1, index) in categoryList"
                :key="category1.categoryId"
                @mouseenter="isEnterIndexItemFn(index)"
                :class="isEnterIndex === index ? 'active' : ''"
              >
                <h3>
                  <a
                    href="javascript:;"
                    :data-category1ID="category1.categoryId"
                    :data-categoryName="category1.categoryName"
                    >{{ category1.categoryName }}</a
                  >
                </h3>
                <div class="item-list clearfix">
                  <div class="subitem">
                    <!-- 二级分类列表 -->
                    <dl
                      class="fore"
                      v-for="category2 in category1.categoryChild"
                      :key="category2.categoryId"
                    >
                      <dt>
                        <a
                          href="javascript:;"
                          :data-category2ID="category2.categoryId"
                          :data-categoryName="category2.categoryName"
                          >{{ category2.categoryName }}</a
                        >
                      </dt>
                      <dd>
                        <!-- 三级分类列表 -->
                        <em
                          v-for="category3 in category2.categoryChild"
                          :key="category3.categoryId"
                        >
                          <a
                            href="javascript:;"
                            :data-category3ID="category3.categoryId"
                            :data-categoryName="category3.categoryName"
                            >{{ category3.categoryName }}</a
                          >
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>

      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { throttle } from "lodash";
export default {
  name: "TypeNav",
  data() {
    return {
      // 移入的当前一级列表的index
      isEnterIndex: -1,
      // 是否在三级分类列表内
      isInCategory: false,
      // 是否在导航内
      isInNavList: false,
    };
  },
  methods: {
    //1.鼠标移入每一个三级列表后的事件函数(节流之后,这个函数并不是直接使用,而是交给节流函数使用的)
    isEnterIndexItem(index) {
      /* 
        1-1 — 做节流后有个bug，当鼠标移出三级分类列表的时候，仍然会执行最后一次，
            — 可以写一个变量，如果不满足条件就不去执行下面代码
      */
      if (!this.isInCategory) return;
      this.isEnterIndex = index;
    },
    //2.设置一个throttle函数的返回值函数的占位
    isEnterIndexItemFn() {},

    mouseleaveHandler() {
      this.isEnterIndex = -1;
      this.isInCategory = false;
    },

    //编程式路由导航的跳转
    toSearchRoute(e) {
      //获取params参数
      const params = this.$route.params
      // console.log(e.target.nodeName);
      if (e.target.nodeName !== "A") return;
      const { category1id, category2id, category3id, categoryname } =
        e.target.dataset;
      // console.log(category1id,category2id,category3id,categoryname);
      this.$router.push({
        name: "search",
        query: {
          category1Id: category1id,
          category2Id: category2id,
          category3Id: category3id,
          categoryName: categoryname,
        },
        params,
      });
    },
  },
  mounted() {
    /* 
      3.在初始化的时候,就使用throttle把鼠标移入三级列表的事件函数mouseEnterItem进行节流封装
        实例上我已经预先设置了一个方法,对throttle返回的函数进行占位,得到节流返回的函数,交给事件
    */
    this.isEnterIndexItemFn = throttle(this.isEnterIndexItem, 100, {
      leading: true, //指定调用在节流开始前
      trailing: true, //指定调用在节流结束后。
    });
  },
  computed: {
    ...mapState("category", ["categoryList"]),

    // 写一个计算属性判断二级分类列表的隐藏或显示
    isInSearch() {
      //如果在home里面就显示
      if (this.$route.name === "home") return true;
      //如果不再home里面而且鼠标移入了,才能显示
      return this.isInNavList;
    },
  },
  watch: {
    // 监听路由地址的改变，点击导航以后页面跳转，让导航消失
    $route() {
      this.isInNavList = false;
    },
  },
};
</script>

<style lang="less" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          &.active {
            h3 {
              background: red;
            }
            .item-list {
              display: block;
            }
          }
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          /* &:hover {
            .item-list {
              display: block;
            }
          } */
        }
      }
    }
  }
}

.nav-enter,.nav-leave-to{
  opacity: 0;
}
.nav-enter-active,.nav-leave-active{
  transition: all 0.4s linear;
}
</style>>